package com.um.jdy.mp.station.manager;


import com.um.jdy.common.device.code.DeviceCode;
import com.um.jdy.common.device.entity.po.Fee;
import com.um.jdy.common.device.entity.vo.FeeVO;
import com.um.jdy.common.device.service.*;
import com.um.jdy.mp.user.service.SessionUserService;
import com.um.jdy.rpc.enums.DeviceTypeEnum;
import com.um.springboot.starter.entity.Page;
import com.um.springboot.starter.exception.ServiceException;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.manager.BaseManager;
import com.um.springboot.starter.orm.params.ListParams;
import com.um.springboot.starter.orm.params.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class FeeManager extends BaseManager<FeeService> {

    @Autowired
    FeeEbPriceService feeEbPriceService;

    @Autowired
    private FeeEbPowerService feeEbPowerService;

    @Autowired
    private FeeEBoxPriceService feeEBoxPriceService;

    @Autowired
    private FeeEBoxPowerService feeEBoxPowerService;

    @Autowired
    private FeePlaceTimeService feePlaceTimeService;

    @Autowired
    private FeePlaceService feePlaceService;

    @Autowired
    private FeeEvPriceService feeEvPriceService;

    @Autowired
    private FeeEvTimeService feeEvTimeService;

    @Autowired
    private FeePbPriceService feePbPriceService;

    @Autowired
    private FeeEbTimeService feeEbTimeService;

    @Autowired
    private FeeBsbRentService feeBsbRentService;

    @Autowired
    private FeeBsbSwapService feeBsbSwapService;

    @Autowired
    SessionUserService sessionUserService;

    public Response<List<FeeVO>> list(ListParams params) {
        params.getCondition().put("s.com_id", getComId());
        params.getCondition().put("s.platform_merchant_id", sessionUserService.getTokenUserId());
        List list = service.getCMSViewModelListBySqlName("get_fee_list", FeeVO.class, params.getProperties(), params.getValues(), params.getRangeEntitys(), params.getOrderNames(), params.getSortOns());
        return Response.success(list);
    }

    public Response<Page<FeeVO>> page(PageParams params) {
        Page<FeeVO> page = new Page();
        page.setPageNo(params.getPageNo());
        page.setPageSize(params.getPageSize());

        params.getCondition().put("s.com_id", getComId());
        params.getCondition().put("s.platform_merchant_id", sessionUserService.getTokenUserId());
        page = service.getCMSViewModelPageBySqlName(page, "get_fee_page", "get_fee_count",
                FeeVO.class, params.getProperties(), params.getValues(), params.getRangeEntitys(), params.getOrderNames(), params.getSortOns());

        return Response.success(page);
    }

    public Response delete(String id) {

        Fee fee = service.getEntity(new String[]{"device_fee_id", "platform_merchant_id"}, new String[]{id, sessionUserService.getTokenUserId()});
        if(fee == null){
            throw new ServiceException(DeviceCode.NoFee);
        }

        if(fee.getDeviceType().equals(DeviceTypeEnum.EB.getKey())){

            feeEbPriceService.delete("device_fee_id",id);
            feeEbPowerService.delete("device_fee_id",id);
            feeEbTimeService.delete("device_fee_id",id);

        }else if(fee.getDeviceType().equals(DeviceTypeEnum.EBox.getKey())){

            feeEBoxPriceService.delete("device_fee_id",id);
            feeEBoxPowerService.delete("device_fee_id",id);
            feePlaceService.delete("device_fee_id",id);
            feePlaceTimeService.delete("device_fee_id",id);


        }else if(fee.getDeviceType().equals(DeviceTypeEnum.EV.getKey())){

            feeEvPriceService.delete("device_fee_id",id);
            feeEvTimeService.delete("device_fee_id",id);
            feePlaceService.delete("device_fee_id",id);
            feePlaceTimeService.delete("device_fee_id",id);

        }else if(fee.getDeviceType().equals(DeviceTypeEnum.PB.getKey())){

            feePbPriceService.delete("device_fee_id",id);

        }else if(fee.getDeviceType().equals(DeviceTypeEnum.BSB.getKey())){

            feeBsbRentService.delete("device_fee_id",id);
            feeBsbSwapService.delete("device_fee_id",id);

        }

        boolean result = this.service.delete(new String[]{"device_fee_id", "platform_merchant_id"}, new String[]{id, sessionUserService.getTokenUserId()});
        return result ? Response.success() : Response.failed();
    }


}
